﻿/**
* @authors <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.ng.ui {
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class cButton extends Sprite	{
		
		private var __Action:String;		
		private var __Skin:Sprite;		
		private var __txtField:TextField;
		private var __Format:TextFormat;
		private var __Initialized:Boolean = false;
			
		public function cButton(action:String, label:String, x:Number = 0, y: Number = 0 ) {
			__txtField = new TextField();
			__txtField.text = label;
			__Action = action;
			this.visible = false;
			this.x = x;
			this.y = y;
		}		
		public function get Action():String {
			return __Action;
		}
		public function set Label(label: String):void {
			__txtField.replaceText(0,label.length);
		}
		public function set Format(format: TextFormat):void {
			__Format= format;
		}
		public function set Skin(skin: Sprite):void {
			__Skin= skin;
		}		
		public function Hide():void {
			this.visible = false;
			//buttonMode = false;
		}		
		public function Show():void {
			if (!__Initialized) {				
				if (!__Format) {
					InitDefaultFormat();					
				}
				if (!__Skin) {
					InitDefaultSkin();
				}
				InitTextField();
				__Initialized = true;
			}
			this.visible = true;
			buttonMode = true;
		}
		public function set Enabled(b:Boolean):void {
			if (__Initialized) {
				if (b) {
					__Skin.alpha = 1;
					__txtField.alpha = 1;
					buttonMode = true;
				} else {
					__Skin.alpha = 0.3;
					__txtField.alpha = 0.3;
					buttonMode = false;
				}
			}
		}
		private function InitDefaultFormat():void {
			__Format = new TextFormat();
			__Format.align = "left";		
			__Format.color = 0x000000;
		}
		private function InitDefaultSkin():void {
			__Skin = new Sprite();
			__Skin.graphics.lineStyle(1,0xBFBFBF);
			__Skin.graphics.beginFill(0xDEDBDE);
			__Skin.graphics.drawRect(0,0,__txtField.text.length*10,20);
			__Skin.graphics.endFill();
			__Skin.x = 0;
			__Skin.y = 0;
			addChild(__Skin);
		}
		private function InitTextField():void {
			__txtField.x = 0;
			__txtField.y = 0;
			__txtField.width = __txtField.text.length*10;	
			__txtField.height  = 20;							
			__txtField.wordWrap = true;
			//__txtField.multiline = true;				
			//__txtField.border = true;
			addChild(__txtField);
			__txtField.setTextFormat(__Format);			
			__txtField.selectable = false;
			__txtField.mouseEnabled = false;
		}
	}
}